package com.example.sls.dao.social;

import com.example.sls.pojo.social.Activities;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 活动表(Activities)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-18 10:54:56
 */
public interface ActivitiesDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from activities where id=#{id}")
  Activities queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param activities 查询条件
   * @param pageSize   分页对象
   * @param offset     偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from activities where 1=1" +
          " and (id=#{activities.id} or (isnull(#{activities.id}) or #{activities.id}=0))" +
          " and (user_id=#{activities.userId} or (isnull(#{activities.userId}) or #{activities.userId}=0))" +
          " and (activity_type=#{activities.activityType} or (isnull(#{activities.activityType}) or #{activities.activityType}=''))" +
          " and (start_time=#{activities.startTime} or (isnull(#{activities.startTime}) or #{activities.startTime}=''))" +
          " and (end_time=#{activities.endTime} or (isnull(#{activities.endTime}) or #{activities.endTime}=''))" +
          " and (location=#{activities.location} or (isnull(#{activities.location}) or #{activities.location}=''))" +
          " and (max_participants=#{activities.maxParticipants} or (isnull(#{activities.maxParticipants}) or #{activities.maxParticipants}=0))" +
          " and (created_at=#{activities.createdAt} or (isnull(#{activities.createdAt}) or #{activities.createdAt}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<Activities> queryAllByLimit(Activities activities, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from activities")
  long count();

  /**
   * 新增数据
   *
   * @param activities 实例对象
   * @return 影响行数
   */
  @Insert("insert into activities (id,user_id,activity_type,start_time,end_time,location,max_participants,created_at) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(#{userId}!=0 and !isnull(#{userId}),#{userId},null)," +
          "if(#{activityType}!='' and !isnull(#{activityType}),#{activityType},null)," +
          "if(#{startTime}!='' and !isnull(#{startTime}),#{startTime},null)," +
          "if(#{endTime}!='' and !isnull(#{endTime}),#{endTime},null)," +
          "if(#{location}!='' and !isnull(#{location}),#{location},null)," +
          "if(#{maxParticipants}!=0 and !isnull(#{maxParticipants}),#{maxParticipants},null)," +
          "if(#{createdAt}!='' and !isnull(#{createdAt}),#{createdAt},null)" +
          ")")
  int insert(Activities activities);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param activities 实例对象
   * @return 影响行数
   */
  @Update("update activities set " +
          "user_id=if(isnull(#{userId}) or #{userId}=0,user_id,#{userId})," +
          "activity_type=if(isnull(#{activityType}) or #{activityType}='',activity_type,#{activityType})," +
          "start_time=if(isnull(#{startTime}) or #{startTime}='',start_time,#{startTime})," +
          "end_time=if(isnull(#{endTime}) or #{endTime}='',end_time,#{endTime})," +
          "location=if(isnull(#{location}) or #{location}='',location,#{location})," +
          "max_participants=if(isnull(#{maxParticipants}) or #{maxParticipants}=0,max_participants,#{maxParticipants})," +
          "created_at=if(isnull(#{createdAt}) or #{createdAt}='',created_at,#{createdAt})" +
          " where id=#{id}")
  int update(Activities activities);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from activities where id=#{id}")
  int deleteById(Integer id);
}

